package me.jessyan.mvparms.demo.di.module.integral;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.CustomPopupWindow;

import javax.inject.Named;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import dagger.Module;
import dagger.Provides;

import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.mvp.contract.integral.MyExchangeContract;
import me.jessyan.mvparms.demo.mvp.model.integral.MyExchangeModel;
import me.jessyan.mvparms.demo.utils.PerfectClickListener;


@Module
public class MyExchangeModule {
    private MyExchangeContract.View view;

    /**
     * 构建MyExchangeModule时,将View的实现类传进来,这样就可以提供View的实现类给presenter
     *
     * @param view
     */
    public MyExchangeModule(MyExchangeContract.View view) {
        this.view = view;
    }

    @ActivityScope
    @Provides
    MyExchangeContract.View provideMyExchangeView() {
        return this.view;
    }

    @ActivityScope
    @Provides
    MyExchangeContract.Model provideMyExchangeModel(MyExchangeModel model) {
        return model;
    }

    @ActivityScope
    @Provides
    @Named("barCodePop")
    CustomPopupWindow QRCodePop(){

        View content = LayoutInflater.from(view.getActivity())
                .inflate(R.layout.pop_coupon_bar_code,null);
        CustomPopupWindow popupWindow = CustomPopupWindow.builder()
                .isFocus(true)
                .isOutsideTouch(true)
                .isWrap(true)
                .contentView(content)
                .customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {


                        TextView endTv = contentView.findViewById(R.id.back_tv);
                        ImageView barCodeIv = contentView.findViewById(R.id.bar_code_iv);

                        Observable.just("654 156 1561")
                                .subscribeOn(Schedulers.io())
                                .map(new Function<String, Bitmap>() {
                                    @Override
                                    public Bitmap  apply(String s) throws Exception {
                                        return QRCodeEncoder.syncEncodeBarcode(s, ArmsUtils.dip2px(view.getActivity().getApplicationContext(),192),
                                                ArmsUtils.dip2px(view.getActivity().getApplicationContext(),61),ArmsUtils.dip2px(view.getActivity().getApplicationContext(),14));
                                    }
                                })
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Bitmap >() {
                            @Override
                            public void accept(Bitmap  o) throws Exception {
                                barCodeIv.setImageBitmap(o);
                            }
                        });

                        endTv.setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                view.dismissPop();
                            }
                        });

                    }
                }).build();
        //PopupWindow全屏显示
        popupWindow.setClippingEnabled(false);
        return popupWindow;
    }
}